package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags="菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> pageSelect(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品...");
        PageResult pageResult=dishService.pageSelect(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    //TODO 好像有bug
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    @CacheEvict(cacheNames = "selectById",key = "#categoryId")
    public Result<List<Dish>> selectById(String categoryId){
        log.info("根据分类id查询菜品...");
        List<Dish> list=dishService.selectById(categoryId);
        return Result.success(list);
    }

    /**
     *添加菜品
     * @param dishDTO
     * @return
     */
    @PostMapping()
    @ApiOperation("添加菜品")
    @CacheEvict(cacheNames = "addDish",key = "#dishDTO.categoryId")
    public Result addDish(DishDTO dishDTO){
        log.info("添加菜品...");
        dishService.addDish(dishDTO);
        return Result.success();
    }

    /**
     * 根据id查询回显信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id回显菜品信息")
    @CacheEvict(cacheNames = "find",key = "#id")
    public Result<DishVO> find(@PathVariable Long id){
        log.info("根据id回显菜品信息...，");
        DishVO dishVO=dishService.find(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    //TODO 有bug
    @PutMapping()
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "updateDish",key = "#dishDTO.id")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品...");
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 起售或停售商品
     * @param status
     * @return
     */
    @PostMapping("status/{status}")
    @ApiOperation("起售停售商品")
    @CacheEvict(cacheNames = "startOrStop",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("起售或停售商品...");
        dishService.startOrStop(status,id);
        return Result.success();
        }

    @DeleteMapping()
    @ApiOperation("批量删除菜品")
    @CacheEvict(cacheNames = "delete",allEntries = true)
    public Result delete(@PathParam("ids") String ids){
        log.info("批量删除菜品...");
        dishService.delete(ids);
        return Result.success();
    }
}
